package com.coastwe.dahua.chapter04;

public class ArrayQueue {
    private int[] data;
    private int n;
    private int head;
    private int tail;

    public ArrayQueue(int capacity) {
        this.data = new int[capacity];
        this.n = capacity;
        this.head = 0;
        this.tail = 0;
    }

    public boolean enqueue(int value) {
        if (tail == n) {
            if (head == 0) {
                System.out.println("队列已经满了");
                return false;
            }
            for (int i = head; i < tail; i++) {
                data[i - head] = data[i];
            }
            this.tail -= head;
            head = 0;
        }
        this.data[tail] = value;
        this.tail++;
        return true;
    }

    public int dequeue() {
        if (head == tail) {
            System.out.println("队列已经空了");
            return -1;
        }
        int value = this.data[head++];
        return value;
    }

    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(5);
        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);
        queue.enqueue(4);
        queue.enqueue(5);
        queue.enqueue(5);
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);
        queue.enqueue(4);
        queue.enqueue(5);
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());

    }
    
}